package com.le.ui;

import com.le.component.BackGroundPanel;
import com.le.util.InsertDataToSql;
import com.le.util.Query;
import com.le.util.RandomAdd;
import com.le.util.ScreenUtils;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Map;

public class StudentUpdatePage {
    String sno;
    String hostIdAddress;
    JFrame jFrame;
    BackGroundPanel backGroundPanel = new BackGroundPanel(new ImageIcon("images//背景-10.png").getImage());
    final int WIDTH = 720;
    final int HEIGHT = 480;
    Map<String, String[]> majors = RandomAdd.MAJOR;

    JTextField nameTf = new JTextField();

    JLabel genderLabel = new JLabel("请选择性别:");
    JRadioButton male = new JRadioButton("男");
    JRadioButton female = new JRadioButton("女");

    JLabel nameLabel = new JLabel("新  姓  名:");

    JLabel yearLabel = new JLabel("出生年:");
    JLabel monthLabel = new JLabel("出生月:");
    JLabel dayLabel = new JLabel("出生日:");
    JComboBox<String> yearsComboBox = new JComboBox<>();
    JComboBox<String> monthComboBox = new JComboBox<>();
    JComboBox<String> daysComboBox = new JComboBox<>();

    JButton add = new JButton("确      定");

    public StudentUpdatePage(String hostIdAddress, String sno) {
        this.hostIdAddress = hostIdAddress;
        this.sno = sno;
    }

    public StudentUpdatePage(String hostIdAddress) {
        this.hostIdAddress = hostIdAddress;
    }


    public void init() {
        jFrame = new JFrame("学号为：" + sno + "的学生信息修改");
        //组装视图
        jFrame.setIconImage(new ImageIcon("images/添加学生.png").getImage());
        jFrame.setBounds((ScreenUtils.getScreenWidth() - WIDTH) / 2,
                (ScreenUtils.getScreenHeight() - HEIGHT) / 2, WIDTH, HEIGHT);

        Box nameBox = Box.createHorizontalBox();
        nameBox.add(nameLabel);
        nameBox.add(Box.createHorizontalStrut(10));
        nameBox.add(nameTf);

        Box genderChoiceBox = Box.createHorizontalBox();
        ButtonGroup genderButtonGroup = new ButtonGroup();
        genderButtonGroup.add(male);
        genderButtonGroup.add(female);
        genderChoiceBox.add(genderLabel);
        genderChoiceBox.add(Box.createHorizontalStrut(10));
        genderChoiceBox.add(male);
        genderChoiceBox.add(Box.createHorizontalStrut(10));
        genderChoiceBox.add(female);

        Box bornDayBox = Box.createHorizontalBox();
        for (int i = 1980; i < 2023; i++) {
            yearsComboBox.addItem(String.valueOf(i));
        }
        for (int i = 1; i < 13; i++) {
            monthComboBox.addItem(String.valueOf(i));
        }
        for (int i = 1; i < 32; i++) {
            daysComboBox.addItem(String.valueOf(i));
        }
        bornDayBox.add(yearLabel);
        bornDayBox.add(Box.createHorizontalStrut(5));
        bornDayBox.add(yearsComboBox);
        bornDayBox.add(Box.createHorizontalStrut(5));
        bornDayBox.add(monthLabel);
        bornDayBox.add(Box.createHorizontalStrut(5));
        bornDayBox.add(monthComboBox);
        bornDayBox.add(Box.createHorizontalStrut(5));
        bornDayBox.add(dayLabel);
        bornDayBox.add(Box.createHorizontalStrut(5));
        bornDayBox.add(daysComboBox);

        Box btnBox = Box.createHorizontalBox();
        add.setPreferredSize(new Dimension(80, 30));
        btnBox.add(add);

        Box content = Box.createVerticalBox();
        content.add(Box.createVerticalStrut(125));
        content.add(nameBox);
        content.add(Box.createVerticalStrut(20));
        content.add(bornDayBox);
        content.add(Box.createVerticalStrut(20));
        content.add(genderChoiceBox);
        content.add(Box.createVerticalStrut(40));
        content.add(btnBox);

        backGroundPanel.add(content);

        jFrame.add(backGroundPanel);
        jFrame.setPreferredSize(new Dimension(WIDTH, HEIGHT));
        jFrame.setResizable(false);
        jFrame.setDefaultCloseOperation(WindowConstants.HIDE_ON_CLOSE);
        jFrame.setVisible(true);

        //事件处理

        add.addActionListener((ActionEvent e) -> {
            String newStudentName = nameTf.getText().trim();
            String newStudentGender = male.isSelected() ? "男" : "女";
            if (newStudentName.length() == 0) JOptionPane.showMessageDialog(jFrame, "请输入姓名！");
            else if (!(male.isSelected() || female.isSelected())) JOptionPane.showMessageDialog(jFrame, "请选择性别");
            else {
                String newStudentBornDay = yearsComboBox.getSelectedItem().toString() + "-" +
                        monthComboBox.getSelectedItem().toString() + "-" +
                        daysComboBox.getSelectedItem().toString();

                //在数据库里更新学生信息
                String uri =
                        "jdbc:mysql://" + hostIdAddress + ":3306/" +
                                "sams" + "?useSSL=true&characterEncoding=utf-8";
                try {
                    Connection connection = DriverManager.getConnection(uri, "zhang", "123456");
                    Statement statement = connection.createStatement();
                    statement.execute("update student set Sname='" + newStudentName + "', gender='" +
                            newStudentGender + "', bornDay='" + newStudentBornDay + "' where SNO=" + sno);
                    statement.close();
                    JOptionPane.showMessageDialog(jFrame, "更新成功！");
                    jFrame.dispose();
                } catch (SQLException ex) {
                    ex.printStackTrace();
                }
            }
        });
    }
}
